package com.alibaba.citrus.ots.b2b.trade.order.repository.impl;

import javax.annotation.Resource;

import com.alibaba.citrus.ots.b2b.trade.order.repository.DeliverOrderWriteRepository;

import com.epoch.app.otsb2btradecenter.domain.deliveryorder.dto.CreateDeliveryOrderResponse;
import com.epoch.app.otsb2btradecenter.domain.deliveryorder.dto.LoadDeliveryOrderRequest;
import com.epoch.app.otsb2btradecenter.domain.deliveryorder.dto.UpdateDeliveryOrderResponse;
import com.epoch.app.otsb2btradecenter.domain.deliveryorder.model.DeliveryOrder;
import com.epoch.app.otsb2btradecenter.domain.deliveryorder.service.DeliveryOrderService;
import com.epoch.app.otsb2btradecenter.sdo.DeliverOrderSDO;
import org.springframework.stereotype.Component;

@Component
public class DeliverOrderWriteRepositoryImpl implements DeliverOrderWriteRepository {

    @Resource
    private DeliveryOrderService deliveryOrderService;

    @Override
    public Boolean modify(DeliverOrderSDO deliverOrderSDO) {
        LoadDeliveryOrderRequest loadDeliveryOrderRequest = new LoadDeliveryOrderRequest();
        loadDeliveryOrderRequest.setId(String.valueOf(deliverOrderSDO.getId()));
        DeliveryOrder deliveryOrder = deliveryOrderService.loadDeliveryOrder(loadDeliveryOrderRequest);
        deliveryOrder.setDeliveryStatus(String.valueOf(deliverOrderSDO.getDeliveryStatus()));
        UpdateDeliveryOrderResponse updateDeliveryOrderResponse = deliveryOrderService.updateDeliveryOrder(deliveryOrder);
        if (updateDeliveryOrderResponse.getCount() != 1) {
            throw new RuntimeException("modify DeliveryOrder effect rows not 1!");
        }
        return true;
    }

    @Override
    public String create(DeliverOrderSDO deliverOrderSDO) {
        DeliveryOrder deliveryOrder = converter(deliverOrderSDO);
        CreateDeliveryOrderResponse response = deliveryOrderService.createDeliveryOrder(deliveryOrder);
        return response.getLastInsertId();
    }

    public static DeliveryOrder converter(DeliverOrderSDO deliverOrderSDO) {
        DeliveryOrder deliveryOrder = new DeliveryOrder();
        deliveryOrder.setOrderId(deliverOrderSDO.getOrderId());
        deliveryOrder.setTenantId(deliverOrderSDO.getTenantId());
        deliveryOrder.setCustomerCode(deliverOrderSDO.getCustomerCode());
        deliveryOrder.setDeliveryAddressCode(deliverOrderSDO.getDeliveryPartCode());
        deliveryOrder.setDeliveryDetailAddress(deliverOrderSDO.getDetailAddress());
        //发货仓
        deliveryOrder.setWarehouseCode(deliverOrderSDO.getWarehouseCode());
        deliveryOrder.setWarehouseName(deliverOrderSDO.getWarehouseName());
        deliveryOrder.setReceiveAddressCode(deliveryOrder.getReceiveAddressCode());
        deliveryOrder.setReceiveDetailAddress(deliverOrderSDO.getDetailAddress());
        deliveryOrder.setContact(deliverOrderSDO.getContact());
        deliveryOrder.setContactPhone(deliverOrderSDO.getContactPhone());
        deliveryOrder.setDeliveryTime(deliverOrderSDO.getDeliveryTime());
        deliveryOrder.setReceivedTime(deliverOrderSDO.getReceiveTime());
        deliveryOrder.setDeliveryStatus(deliverOrderSDO.getDeliveryStatus().toString());
        deliveryOrder.setOutDeliveryOrderId(deliverOrderSDO.getOutDeliveryId());
        deliveryOrder.setPostCode(deliverOrderSDO.getPostcode());
        deliveryOrder.setTransportModeCode(deliverOrderSDO.getTransportModeCode());
        deliveryOrder.setTransportModeName(deliverOrderSDO.getTransportModeName());
        deliveryOrder.setSyncVersion(deliverOrderSDO.getSyncVersion());
        deliveryOrder.setGmtModified(deliverOrderSDO.getGmtModified());
        deliveryOrder.setGmtCreate(deliverOrderSDO.getGmtCreate());
        deliveryOrder.setCreator(deliverOrderSDO.getCreator());
        deliveryOrder.setUpdater(deliverOrderSDO.getUpdater());
        return deliveryOrder;
    }
}